//import java.util.Random;
//import java.util.random.*;
//
//class Ticketing{
//    private static int count;
//    public Ticketing(int count){
//        this.count = count;
//    }
//    public synchronized static void getTicket(int num){
//        if(count-num>=0){
//            count-=num;
//        }else{
//            System.out.println("票数不够");
//        }
//    }
//
//    public synchronized static int getCount() {
//        return count;
//    }
//}
//
//class Mythread implements Runnable{
//    private final Ticketing ticketing;
//    private final Random random;
//    public Mythread(Ticketing ticketing){
//        this.ticketing = ticketing;
//        random = new Random();
//    }
//    @Override
//    public void run(){
//        while(true){
//            synchronized(Ticketing.class){
//                if(Ticketing.getCount()<=0){
//                    System.out.println(Thread.currentThread()+"发现票已经没了，线程结束！");
//                    break;
//                }
//            }
//            int num = random.nextInt(5)+1;
//            Ticketing.getTicket(num);
//            try{
//                Thread.sleep(random.nextInt(200));
//            }catch (InterruptedException e){
//                e.printStackTrace();
//            }
//        }
//    }
//}
//
//
//public class Main {
//    public static void main(String[] args) {
//        Ticketing ticketing = new Ticketing(100);
//        Thread t1 = new Thread(new Mythread(ticketing),"线程1");
//        Thread t2 = new Thread(new Mythread(ticketing),"线程2");
//        Thread t3 = new Thread(new Mythread(ticketing),"线程3");
//        Thread t4 = new Thread(new Mythread(ticketing),"线程4");
//        t1.start();
//        t2.start();
//        t3.start();
//        t4.start();
//    }
//}

import java.util.Random;

class Ticketing {
    private static int count = 200;

    public synchronized static void getTicket(int num) {
        if (count >= num) {
            count -= num;
            System.out.println(Thread.currentThread().getName()+"抢到"+num+"张票,"+"剩余："+count+"张票");
            //return true;
        } else {
            System.out.println(Thread.currentThread().getName() + ": 票数不够");
//            return false;
        }
    }

    public synchronized static int getCount() {
        return count;
    }
}

class MyThread implements Runnable {
    private final Random random;

    public MyThread() {
        random = new Random();
    }

    @Override
    public void run() {
        while (true){
            synchronized (Ticketing.class){
                if(Ticketing.getCount()<=0){
                    System.out.println(Thread.currentThread().getName()+"票数不够。退出");
                    break;
                }
                int num = random.nextInt(5)+1;
                Ticketing.getTicket(num);
                try{
                    Thread.sleep(random.nextInt(200));
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
        }
    }
}

public class Main {
    public static void main(String[] args) {

        Thread t1 = new Thread(new MyThread(), "线程1");
        Thread t2 = new Thread(new MyThread(), "线程2");
        Thread t3 = new Thread(new MyThread(), "线程3");

        t1.start();
        t2.start();
        t3.start();
    }
}
